package com.kh.pajx.sop.dao.sys.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.kh.pajx.sop.base.BaseDaoImpl;
import com.kh.pajx.sop.dao.sys.ISysDictionaryDao;
import com.kh.pajx.sop.domain.SysDictionary;

/**
 * 字典持久化操作接口实现类
 * @author:	azzcsimp
 * @Createdate:	2014年6月27日 下午5:01:34
 */
@Repository
@SuppressWarnings("unchecked")
public class SysDictionaryDaoImpl extends BaseDaoImpl<SysDictionary> implements ISysDictionaryDao{

	
	// ==>1.ISysDictionaryDao重载方法
	
	/**
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#findDicListByType(java.lang.String)
	 */
	@Override
	public List<SysDictionary> findDicListByType(String typeCode) throws Exception {
		return getSession().createQuery(//
				"FROM SysDictionary dic WHERE dic.dicTypeCode=? ")//
				.setParameter(0, typeCode) //
				.list();
	}
	
	// ==>2.IBaseDao重载方法
	
	
	
	
	
	
	
	
	// =========================以下为需要整理或废弃方法===================================	
	/**
	 * 查询所有字典类型列表
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#getTypeList()
	 */
	@Override
	public List<String> getDicTypeNameList() {
		String queryString = "select dic.dicTypeName from SysDictionary dic group by dic.dicTypeName ";
		return findListByHql2(queryString);
	}
	
	/**
	 * 获取字典类型列表
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#getDicTypeList()
	 */
	@Override
	public List<SysDictionary> getDicTypeList() {
		String queryString = "from SysDictionary dic group by dic.dicTypeName order by dic.dicTypeName";
		return findListByHql(queryString);
	}
	
	/**
	 * 根据字典类型名称查找字典列表
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#getDicListByTypeName(java.lang.String)
	 */
	@Override
	public List<SysDictionary> getDicListByTypeName(String dicTypeName) {
		String queryString = "FROM SysDictionary dic WHERE dic.dicTypeName=?";
		return findListByHql(queryString, dicTypeName);
	}
	
	/**
	 * 根据类型名称查找字典类型列表
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#getDicTypeListByTypeName()
	 */
	@Override
	public List<?> getDicTypeListByTypeName(String dicTypeName) {
		String whereClause = "";
		//if(!StringUtils.isNullOrEmpty(dicTypeName)){
			whereClause = " where dic.dicTypeName like ? " ; 
		//}
		String queryString = "SELECT dic.dicTypeCode,dic.dicTypeName from SysDictionary dic "+whereClause+" group by dic.dicTypeCode,dic.dicTypeName order by dic.dicTypeCode";
		return findListByHql2(queryString,"%"+dicTypeName+"%");
	}
	
	/**
	 * 校验字典类型编码是否已存在
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#checkRepeatDicType(java.lang.String)
	 */
	@Override
	public int checkRepeatDicTypeCode(String dicTypeCode){
		String queryString = "select count(*) from SysDictionary dic where dic.dicTypeCode=:typeCode";
		Query query = getSession().createQuery(queryString);
		query.setParameter("typeCode", dicTypeCode);
		return ((Number) query.iterate().next()).intValue();
	}
	
	/**
	 * 校验同一字典类型编码下，类型名称是否存在
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#checkRepeatDicTypeName(java.lang.String, java.lang.String)
	 */
	@Override
	public int checkRepeatDicTypeName(String dicTypeCode,String dicTypeName){
		String queryString = "select count(*) from SysDictionary dic where dic.dicTypeCode=:typeCode and dic.dicTypeName=:typeName";
		Query query = getSession().createQuery(queryString);
		query.setParameter("typeCode", dicTypeCode);
		query.setParameter("typeName", dicTypeName);
		return ((Number) query.iterate().next()).intValue();
	}
	
	/**
	 * 校验统一字典类型编码下，字典编码是否已存在
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#checkRepeatDicCode(java.lang.String, java.lang.String)
	 */
	@Override
	public int checkRepeatDicCode(String dicCode,String dicTypeCode){
		// 校验字典重复需要从两个分支来处理
		// ==>1.校验同一字典类型编码不同字典类型名称
		// ==>2.校验同一字典类型下，字典编码是否唯一
		String queryString = "select count(*) from SysDictionary dic where dic.dicTypeCode=:typeCode and dic.dicCode=:code";
		Query query = getSession().createQuery(queryString);
		query.setParameter("typeCode", dicTypeCode);
		query.setParameter("code", dicCode);
		return ((Number) query.iterate().next()).intValue();
	}

	@Override
	public List<SysDictionary> getDicListByTypeCode(String dicTypeCode) {
		return getSession().createQuery("from SysDictionary dic where dic.dicTypeCode=?").setParameter(0, dicTypeCode).list();
	}
	
	/**
	 * 批量删除字典信息
	 * @see com.kh.pajx.sop.dao.sys.ISysDictionaryDao#delete(java.lang.String[])
	 */
	@Override
	public int delete(String[] uuids) {
		String hql = "DELETE SysDictionary dic WHERE dic.dicId in (:dicIds)";
		return getSession().createQuery(hql).setParameterList("dicIds", uuids).executeUpdate();
	}
	
}
